﻿using System;
using System.Data;
using System.IO;
using System.Data;
using System.Collections.Generic;
namespace TFSBuildMonitoringTool
{
    class BuildMonitorSettings
    {
        public static List<string> BuildsStatus= new List<string>();
        public static List<string> TfsMonitorToolControllersToUpdate= new List<string>();
        public static List<string> BuildsReason= new List<string>();
        private static string _tfsurl = string.Empty;

        public BuildMonitorSettings(string xmlFileLocation)
        {
            DataSet xmldataset=new DataSet();
            if (File.Exists(xmlFileLocation))
            {
                try
                {
                    xmldataset.ReadXml(xmlFileLocation);
                    SetConfigurationForBuilds(xmldataset);
                }
                catch(Exception ex)
                {
                    throw new Exception("Wrong XML file at this location: " + xmlFileLocation);
                }
            }
            else
            {
                throw new FileNotFoundException("The XML file for configuration not found at location: " + xmlFileLocation);

            }
            
        }

        public static string Tfsurl
        {
            get { return _tfsurl; }
            set { _tfsurl = value; }
        }

        private static void SetConfigurationForBuilds(DataSet xmldataset)
        {
            if (xmldataset != null)
            {
                int tableCount = xmldataset.Tables.Count;
                for (int i = 0; i < tableCount; i++)
                {
                    string autoUpdate = string.Empty;
                    string timeInterval = string.Empty;
                    try
                    {
                        if (xmldataset.Tables[i].Columns.Count > 1)
                        {
                            autoUpdate = xmldataset.Tables[i].Rows[0][0].ToString();
                            timeInterval = xmldataset.Tables[i].Rows[0][1].ToString();
                        }


                        switch (xmldataset.Tables[i].TableName)
                        {
                            case "BuildStatisticsInProgress":
                                BuildStatisticsInProgressSettings.AutoUpdate = bool.Parse(autoUpdate);
                                BuildStatisticsInProgressSettings.TimeIntervalInSecond = Int32.Parse(timeInterval);
                                break;

                            case "StatusOfAgentAndController":
                                StatusOfAgentAndControllerSettings.AutoUpdate = bool.Parse(autoUpdate);
                                StatusOfAgentAndControllerSettings.TimeIntervalInSecond = Int32.Parse(timeInterval);
                                break;

                            case "TodaysBuildStatus":
                                TodaysBuildStatusSettings.AutoUpdate = bool.Parse(autoUpdate);
                                TodaysBuildStatusSettings.TimeIntervalInSecond = Int32.Parse(timeInterval);
                                break;

                            case "BuildStatisticsByBuildReason":
                                BuildStatisticsByBuildReasonSettings.AutoUpdate = bool.Parse(autoUpdate);
                                BuildStatisticsByBuildReasonSettings.TimeIntervalInSecond = Int32.Parse(timeInterval);
                                break;

                            case "BuildStatisticsByAgentAndController":
                                BuildStatisticsByAgentAndControllerSettings.AutoUpdate = bool.Parse(autoUpdate);
                                BuildStatisticsByAgentAndControllerSettings.TimeIntervalInSecond =
                                    Int32.Parse(timeInterval);
                                break;

                            case "BuildsJob":
                                BuildsJobSettings.AutoUpdate = bool.Parse(autoUpdate);
                                BuildsJobSettings.TimeIntervalInSecond = Int32.Parse(timeInterval);
                                break;

                            case "BuildsStatus":
                                // string Status = xmldataset.Tables[i].Rows[0][0].ToString();
                                BuildsStatus.Clear();
                                string[] StatusSplits = xmldataset.Tables[i].Rows[0][0].ToString().Split(',');
                                foreach (string statusSplit in StatusSplits)
                                {
                                    BuildsStatus.Add(statusSplit.Trim());
                                }

                                break;
                            case "TfsMonitorToolControllersToUpdate":
                                TfsMonitorToolControllersToUpdate.Clear();
                                // string controllerToUpdate = xmldataset.Tables[i].Rows[0][0].ToString();
                                string[] controllersToUpdate = xmldataset.Tables[i].Rows[0][0].ToString().Split(',');
                                foreach (string controllerToUpdate in controllersToUpdate)
                                {
                                    TfsMonitorToolControllersToUpdate.Add(controllerToUpdate.Trim());
                                }

                                break;
                            case "BuildReason":
                                // string strBuildReason = xmldataset.Tables[i].Rows[0][0].ToString();
                                BuildsReason.Clear();
                                string[] strBuildsReason = xmldataset.Tables[i].Rows[0][0].ToString().Split(',');
                                foreach (string buildReason in strBuildsReason)
                                {
                                    BuildsReason.Add(buildReason.Trim());
                                }

                                break;

                            case "BuildStatisticsToGet":

                                BuildStatisticsForAPeriod.Days = Int32.Parse(xmldataset.Tables[i].Rows[0][0].ToString());
                                BuildStatisticsForAPeriod.Months =
                                    Int32.Parse(xmldataset.Tables[i].Rows[0][1].ToString());
                                BuildStatisticsForAPeriod.Years = Int32.Parse(xmldataset.Tables[i].Rows[0][2].ToString());
                                break;
                            case "TFSServerURL":
                                Tfsurl = xmldataset.Tables[i].Rows[0][0].ToString();
                                break;

                            default:
                                break;
                        }
                    }
                    catch (Exception exception)
                    {
                        throw new Exception(exception.Message);
                    }
                }
            }
            else
            {
                throw new ArgumentNullException("The XML Settings file not having any values!");
            }
        }
    }

  }
